/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PageSetupPanel.java
 *
 * Created on Oct 8, 2010, 10:36:48 AM
 */
package project.jpdftools.headerfooter.ui;

import java.awt.Toolkit;
import java.util.logging.Level;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JTextField;
import project.jpdftools.headerfooter.Footer;
import project.jpdftools.headerfooter.Header;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;

/**
 *
 * @author Mido
 */
public class HeaderFooterUI extends JDialog {

    private static boolean is_component_visible = false;
    private JTextField[] text_fields;
    private JCheckBox[] check_boxes;
    private String[] header_footer_strings;

    /**
     * Creates new form PageSetupPanel
     */
    public HeaderFooterUI() {
        // check if the frame is already visible
        if (is_component_visible == false) {
            initComponents();

            // center the component
            project.jpdftools.toolkit.Toolkit.centerWindow(this, this.getHeight(), this.getWidth());
            this.setAlwaysOnTop(true);

            // set tab names
            tp_header_footer.setTitleAt(0, LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_L_HEADER"));
            tp_header_footer.setTitleAt(1, LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_L_FOOTER"));

            // intialize arrays
            text_fields = new JTextField[]{tf_left_header, tf_center_header, tf_right_header, tf_left_footer, tf_center_footer, tf_right_footer};
            check_boxes = new JCheckBox[]{cb_left_header, cb_center_header, cb_right_header, cb_left_footer, cb_center_footer, cb_right_footer};
            header_footer_strings = new String[]{Header.getLeft_header(), Header.getCenter_header(), Header.getRight_header(),
                Footer.getLeft_footer(), Footer.getCenter_footer(), Footer.getRight_footer()};

            // load previously saved values
            loadParameters();
            // indicate that the frame is visible
            is_component_visible = true;
            LoggerManager.getLogger().log(Level.INFO, "Frame shown \"{0}\".", "HeaderFooterUI");
        }
    }

    /**
     * Load previously saved parameters
     */
    private void loadParameters() {
        // load header & footer values
        for (int i = 0; i < 6; i++) {
            if (header_footer_strings[i].isEmpty() != true) {
                // set text
                text_fields[i].setText(header_footer_strings[i]);
                // enable check box
                check_boxes[i].setSelected(true);
                // enable text field
                text_fields[i].setEnabled(true);
            }
        }
    }

    public static boolean IsComponentVisible() {
        return is_component_visible;
    }

    /**
     * Enable/Disable text fields
     *
     * @param index
     */
    private void enableComponent(int index) {
        if (check_boxes[index].isSelected() == true) {
            text_fields[index].setEnabled(true);
        } else {
            text_fields[index].setEnabled(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bt_ok = new javax.swing.JButton();
        bt_cancel = new javax.swing.JButton();
        tp_header_footer = new javax.swing.JTabbedPane();
        p_header = new javax.swing.JPanel();
        tf_right_header = new javax.swing.JTextField();
        tf_left_header = new javax.swing.JTextField();
        tf_center_header = new javax.swing.JTextField();
        cb_left_header = new javax.swing.JCheckBox();
        cb_center_header = new javax.swing.JCheckBox();
        cb_right_header = new javax.swing.JCheckBox();
        p_footer = new javax.swing.JPanel();
        tf_left_footer = new javax.swing.JTextField();
        tf_center_footer = new javax.swing.JTextField();
        tf_right_footer = new javax.swing.JTextField();
        cb_left_footer = new javax.swing.JCheckBox();
        cb_center_footer = new javax.swing.JCheckBox();
        cb_right_footer = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_PANEL_TITLE"));
        setIconImage(Toolkit.getDefaultToolkit().getImage("Data/Icons/Soft Scraps Icons/10.png"));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                HeaderFooterUI.this.windowClosing(evt);
            }
        });

        bt_ok.setText(LocalizationManager.getCurrent_resource().getString("OK"));
        bt_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_okActionPerformed(evt);
            }
        });

        bt_cancel.setText(LocalizationManager.getCurrent_resource().getString("CANCEL"));
        bt_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelActionPerformed(evt);
            }
        });

        tf_right_header.setEnabled(false);

        tf_left_header.setEnabled(false);

        tf_center_header.setEnabled(false);

        cb_left_header.setText(LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_LEFT_HEADER"));
        cb_left_header.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_left_headerActionPerformed(evt);
            }
        });

        cb_center_header.setText(LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_CENTER_HEADER"));
        cb_center_header.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_center_headerActionPerformed(evt);
            }
        });

        cb_right_header.setText(LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_RIGHT_HEADER"));
        cb_right_header.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_right_headerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout p_headerLayout = new javax.swing.GroupLayout(p_header);
        p_header.setLayout(p_headerLayout);
        p_headerLayout.setHorizontalGroup(
            p_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_headerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(p_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cb_left_header)
                    .addComponent(cb_center_header)
                    .addComponent(cb_right_header))
                .addGap(12, 12, 12)
                .addGroup(p_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_left_header, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_center_header, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_right_header, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        p_headerLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tf_center_header, tf_left_header, tf_right_header});

        p_headerLayout.setVerticalGroup(
            p_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_headerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(p_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tf_left_header, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_left_header))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tf_center_header, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_center_header))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p_headerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tf_right_header, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_right_header))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        p_headerLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {tf_center_header, tf_left_header, tf_right_header});

        tp_header_footer.addTab("tab1", p_header);

        tf_left_footer.setEnabled(false);

        tf_center_footer.setEnabled(false);

        tf_right_footer.setEnabled(false);

        cb_left_footer.setText(LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_LEFT_FOOTER"));
        cb_left_footer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_left_footerActionPerformed(evt);
            }
        });

        cb_center_footer.setText(LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_CENTER_FOOTER"));
        cb_center_footer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_center_footerActionPerformed(evt);
            }
        });

        cb_right_footer.setText(LocalizationManager.getCurrent_resource().getString("HEADER_FOOTER_RIGHT_FOOTER"));
        cb_right_footer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_right_footerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout p_footerLayout = new javax.swing.GroupLayout(p_footer);
        p_footer.setLayout(p_footerLayout);
        p_footerLayout.setHorizontalGroup(
            p_footerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_footerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(p_footerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cb_left_footer)
                    .addComponent(cb_center_footer)
                    .addComponent(cb_right_footer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(p_footerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_left_footer, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_center_footer, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_right_footer, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        p_footerLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tf_center_footer, tf_left_footer, tf_right_footer});

        p_footerLayout.setVerticalGroup(
            p_footerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p_footerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(p_footerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tf_left_footer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_left_footer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p_footerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tf_center_footer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_center_footer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(p_footerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tf_right_footer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_right_footer))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        p_footerLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {tf_center_footer, tf_left_footer, tf_right_footer});

        tp_header_footer.addTab("tab2", p_footer);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tp_header_footer)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_ok)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tp_header_footer, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_ok)
                    .addComponent(bt_cancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        is_component_visible = false;
        LoggerManager.getLogger().log(Level.INFO, "Frame hidden \"{0}\".", "HeaderFooterUI");
    }//GEN-LAST:event_bt_cancelActionPerformed

    private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
        // left header
        if (cb_left_header.isSelected() == true) {
            Header.setLeft_header(tf_left_header.getText());
        } else {
            Header.setLeft_header("");
        }
        // center header
        if (cb_center_header.isSelected() == true) {
            Header.setCenter_header(tf_center_header.getText());
        } else {
            Header.setCenter_header("");
        }
        // right header
        if (cb_right_header.isSelected() == true) {
            Header.setRight_header(tf_right_header.getText());
        } else {
            Header.setRight_header("");
        }
        // left footer
        if (cb_left_footer.isSelected() == true) {
            Footer.setLeft_footer(tf_left_footer.getText());
        } else {
            Footer.setLeft_footer("");
        }
        // center footer
        if (cb_center_footer.isSelected() == true) {
            Footer.setCenter_footer(tf_center_footer.getText());
        } else {
            Footer.setCenter_footer("");
        }
        // right footer
        if (cb_right_footer.isSelected() == true) {
            Footer.setRight_footer(tf_right_footer.getText());
        } else {
            Footer.setRight_footer("");
        }

        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        is_component_visible = false;
        LoggerManager.getLogger().log(Level.INFO, "Header/Footer changed.");
    }//GEN-LAST:event_bt_okActionPerformed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        bt_cancelActionPerformed(null);
}//GEN-LAST:event_windowClosing

    private void cb_left_footerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_left_footerActionPerformed
        enableComponent(3);
    }//GEN-LAST:event_cb_left_footerActionPerformed

    private void cb_center_headerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_center_headerActionPerformed
        enableComponent(1);
    }//GEN-LAST:event_cb_center_headerActionPerformed

    private void cb_left_headerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_left_headerActionPerformed
        enableComponent(0);
    }//GEN-LAST:event_cb_left_headerActionPerformed

    private void cb_right_headerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_right_headerActionPerformed
        enableComponent(2);
    }//GEN-LAST:event_cb_right_headerActionPerformed

    private void cb_center_footerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_center_footerActionPerformed
        enableComponent(4);
    }//GEN-LAST:event_cb_center_footerActionPerformed

    private void cb_right_footerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_right_footerActionPerformed
        enableComponent(5);
    }//GEN-LAST:event_cb_right_footerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancel;
    private javax.swing.JButton bt_ok;
    private javax.swing.JCheckBox cb_center_footer;
    private javax.swing.JCheckBox cb_center_header;
    private javax.swing.JCheckBox cb_left_footer;
    private javax.swing.JCheckBox cb_left_header;
    private javax.swing.JCheckBox cb_right_footer;
    private javax.swing.JCheckBox cb_right_header;
    private javax.swing.JPanel p_footer;
    private javax.swing.JPanel p_header;
    private javax.swing.JTextField tf_center_footer;
    private javax.swing.JTextField tf_center_header;
    private javax.swing.JTextField tf_left_footer;
    private javax.swing.JTextField tf_left_header;
    private javax.swing.JTextField tf_right_footer;
    private javax.swing.JTextField tf_right_header;
    private javax.swing.JTabbedPane tp_header_footer;
    // End of variables declaration//GEN-END:variables
}
